package com.wangsh.usersCenter.users.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wangsh.usersCenter.common.pojo.ApiResponse;
import com.wangsh.usersCenter.common.pojo.ApiResponseEnum;
import com.wangsh.usersCenter.common.service.BaseServiceImpl;
import com.wangsh.usersCenter.common.util.ConstatFinalUtil;
import com.wangsh.usersCenter.common.util.EncryptUtil;
import com.wangsh.usersCenter.users.dao.IAAdminsDao;
import com.wangsh.usersCenter.users.pojo.AAdmins;
import com.wangsh.usersCenter.users.pojo.AMenu;
import com.wangsh.usersCenter.users.pojo.ARoleMenu;
import com.wangsh.usersCenter.users.pojo.AUsers;
import com.wangsh.usersCenter.users.service.INoLoginOperService;
import com.wangsh.usersCenter.users.service.IRbacDbService;
import com.wangsh.usersCenter.users.service.IUsersDbService;
import com.wangsh.usersCenter.users.service.IUsersOperService;

/**
 * 登陆后的一些操作 一个Controller对应一个Service操作
 * 
 * @author wangsh
 * @param <T>
 */
@Service("usersOperService")
public class UsersOperServiceImpl<T> extends BaseServiceImpl implements IUsersOperService
{
	@Resource
	private IUsersDbService usersDbService;
	@Resource
	private INoLoginOperService noLoginOperService;
	@Resource
	private IRbacDbService rbacDbService;

	@Override
	public ApiResponse<Object> logout(Map<String, Object> paramsMap)
	{
		return null;
	}

	@Override
	public ApiResponse<AUsers> usersUpdate(Map<String, Object> paramsMap)
	{
		/* 获取用户的Id */
		String id = paramsMap.get("id") + "";
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		/* 清空信息 */
		apiResponse.setInfo("", Collections.EMPTY_MAP);
		return apiResponse;
	}

	@Override
	public ApiResponse<Object> operUsersUpdateSubmit(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 获取用户的Id */
		String id = paramsMap.get("id") + "";

		String operType = paramsMap.get("operType") + "";

		boolean upflag = false;
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ApiResponse<AUsers> apiResponseRes = this.usersDbService.findOneUsersService(condMap);
		AUsers users = apiResponseRes.getDataOneJava();
		if ("updateInfo".equalsIgnoreCase(operType))
		{
			String nickName = paramsMap.get("nickName") + "";
			String trueName = paramsMap.get("trueName") + "";
			String birthdayStr = paramsMap.get("birthdayStr") + "";
			String address = paramsMap.get("address") + "";
			String qq = paramsMap.get("qq") + "";
			String phone = paramsMap.get("phone") + "";
			String regionId = paramsMap.get("regionId") + "";
			String sex = paramsMap.get("sex") + "";
			String weiXin = paramsMap.get("weiXin") + "";
			String education = paramsMap.get("education") + "";

			/* 根据不同的请求跳转不同的页面 */
			users.setNickName(nickName);
			users.setTrueName(trueName);
			users.setBirthday(this.dateFormatUtil.strDateTime(birthdayStr));
			users.setAddress(address);
			users.setQq(qq);
			users.setPhone(phone);
			users.setWeiXin(weiXin);
			users.setRegionId(Integer.valueOf(regionId));
			users.setSex(Byte.valueOf(sex));
			users.setEducation(Byte.valueOf(education));

			upflag = true;
		} else if ("updatePass".equalsIgnoreCase(operType))
		{
			String password = paramsMap.get("password") + "";
			String souPass = paramsMap.get("souPass") + "";

			if (this.encryptUtil.checkStr(souPass, users.getPassword()))
			{
				users.setPassword(this.encryptUtil.encodeStr(password));
				users.setSouPass(password);
				upflag = true;
			} else
			{
				/* 原密码不正确 */
				apiResponse.setCode(ApiResponseEnum.INFO_SOUPASS_INCORRECT.getStatus());
			}
		} else if ("updateEmail".equalsIgnoreCase(operType))
		{
			String email = paramsMap.get("email") + "";
			/* 更新邮箱 */
			users.setEmail(email);

			upflag = true;
			if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode())
			{
				/* 修改成功,发送邮件 */
				if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode())
				{
					/* 注册成功,发送邮件 */
					apiResponse = this.noLoginOperService.operEmailVerfySendService(paramsMap);
				}
			}
		} else if ("updatePhoto".equalsIgnoreCase(operType))
		{
			users.setUpdateTime(new Date());
			/* 注册成功,发送邮件 */
			apiResponse = this.usersDbService.updateOneUsersService(users, paramsMap);
			/* 直接就更新了, */
		}

		if (upflag)
		{
			/* 更新用户 */
			users.setUpdateTime(new Date());
			users.setUsersExtend(null);
			apiResponse = this.usersDbService.updateOneUsersService(users);
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}

//查询用户是否存在,拼车service数据	
	@Resource
	private IAAdminsDao adminsDao;

	@Override
	public JSONObject loginAdminsService(JSONObject condJSON)
	{
		/* 返回结果的JSON */
		JSONObject resultJSON = new JSONObject();
		AAdmins admins = new AAdmins();
		EncryptUtil encryptUtil = new EncryptUtil();

		String password = condJSON.getString("password");
		String accountNumber = condJSON.getString("accountNumber");

		if (!"".equals(accountNumber) && accountNumber != null)
		{
			admins = (AAdmins) adminsDao.login(condJSON);
		} else
		{
			admins = null;
		}

		if (admins != null)
		{
			if (admins.getFailedCount() == 10)
			{

				admins.setStatus((byte) 0);
				admins.setFailedTime(new Date());
				adminsDao.update(admins);
			}
			Date NewDate = new Date();
			long nd = NewDate.getTime();
			long fd;
			if (admins.getFailedTime() != null)
			{
				Date fdate = admins.getFailedTime();
				fd = fdate.getTime();
			} else
			{
				fd = 0;
			}
			long i = nd - fd;
			if (admins.getStatus() == 0)
			{
				if (i / (60 * 1000) >= 30)
				{
					admins.setStatus((byte) 1);
					admins.setFailedCount(0);
					adminsDao.update(admins);
				}
			}

			if (encryptUtil.checkStr(password, admins.getPassword()))
			{

				if ("禁用".equals(admins.getStatusStr()))
				{
					resultJSON.put("code", "1");
					resultJSON.put("info", "该账户已被禁用");
				} else
				{
					admins.setLoginCount(admins.getLoginCount() + 1);
					adminsDao.update(admins);
					/* 将管理员的json存储到json中 */
					JSONObject dataJSON = admins.toJSON();
					resultJSON.put("data", dataJSON);
					resultJSON.put("code", "0");
					resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
					resultJSON.put("name", admins.getName());
				}

			} else
			{
				admins.setFailedCount(admins.getFailedCount() + 1);
				adminsDao.update(admins);

				resultJSON.put("code", "2");
				resultJSON.put("info", "密码错误");
				resultJSON.put("name", admins.getName());
			}
		} else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", "用户名错误");
		}
		return resultJSON;
	}

	@Override
	public ApiResponse<Object> verifyAdminsAuthSerivce(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 默认木有权限 */
		apiResponse.setCode(ApiResponseEnum.INFO_AUTH_NOT.getStatus());

		Map<String, Object> condMap = new HashMap<String, Object>();
		/* -------获取参数------ */
		String adminsId = paramsMap.get("adminsId") + "";
		String currentUrl = paramsMap.get("currentUrl") + "";
		/* ------根据管理员id查询管理员对象---- */
		condMap.clear();
		condMap.put("id", adminsId);
		ApiResponse<AAdmins> adminsResponse = this.usersDbService.findOneAdminsService(condMap);
		AAdmins admins = adminsResponse.getDataOneJava();

		/* 根据角色id查询角色菜单表 */
		condMap.clear();
		condMap.put("roleId", admins.getRoleId());
		condMap.put("status", 1);
		ApiResponse<ARoleMenu> roleMenuResponse = this.rbacDbService.findCondListRoleMenuService(null, condMap);
		List<ARoleMenu> roleMenuList = roleMenuResponse.getDataListJava();
		/* 循环集合 */
		for (Iterator<ARoleMenu> iterator = roleMenuList.iterator(); iterator.hasNext();)
		{
			ARoleMenu roleMenu = (ARoleMenu) iterator.next();
			AMenu menu = roleMenu.getMenu();
			if (currentUrl.indexOf(menu.getUrl()) != -1)
			{
				/* 说明有权限 */
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				break;
			}
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	/*获取该角色所拥有的可用的菜单*/
	@Override
	public ApiResponse<Object> getRoleMenuSerivce(Map<String, Object> paramsMap)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();

		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);

		String roleId = (String) paramsMap.get("roleId");
		paramsMap.put("status", 1);
		Map<String, List<Object>> dataList = new HashMap<String, List<Object>>();
		List<Object> dataListJava = new ArrayList<Object>();
		String roleEnableMenuList = (String) this.redisUtil.get(roleId);
		apiResponse.setDataList(dataList);
		if (roleEnableMenuList == null)
		{
			ApiResponse<ARoleMenu> findCondListRoleMenuService = this.rbacDbService.findCondListRoleMenuService(null,
					paramsMap);
			List<ARoleMenu> roleMenuList = findCondListRoleMenuService.getDataListJava();
			for (Iterator<ARoleMenu> iterator = roleMenuList.iterator(); iterator.hasNext();)
			{
				ARoleMenu roleMenu = (ARoleMenu) iterator.next();
				AMenu menu = roleMenu.getMenu();
				dataListJava.add(menu);
			}
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), dataListJava);
			this.redisUtil.put(roleId + "", apiResponse.toJSON().toJSONString());
		} else
		{
			JSONObject jsonObject = JSON.parseObject(roleEnableMenuList);
			Object menuListStr = jsonObject.get("data");
			JSONObject parseObject = JSON.parseObject(String.valueOf(menuListStr));
			Object object = parseObject.get("list");
			List<AMenu> parseArray = JSON.parseArray(String.valueOf(object), AMenu.class);
			List<T> menuList = (List<T>) parseArray;
			dataList.put(ApiResponseEnum.NAME_LIST.getName(), (List<Object>) menuList);
		}
		return apiResponse;
	}

}
